package com.itranswarp.learnjava;

/**
 * Learn Java from https://www.liaoxuefeng.com/
 * 
 * @author liaoxuefeng
 */
public class Main {

	public static void main(String[] args) throws Exception {
		String name = "Xiao Ming";
		int age = 20;
		Person p = new Person();
		// TODO: 利用反射给name和age字段赋值:
		Class c = p.getClass();
		java.lang.reflect.Field[] fs = c.getDeclaredFields();
		for(java.lang.reflect.Field f:fs){
			f.setAccessible(true);
			switch(f.getName()) {
			case"name":{
				f.set(p,name);
				break;
			}
			case"age":{
				f.set(p,age);
				break;
			}
			}
		}
		System.out.println(p.getName()); // "Xiao Ming"
		System.out.println(p.getAge()); // 20
	}
}
